package com.fly.utils;

import java.util.ResourceBundle;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;

public class DataSourceUtils
{
    /**
     ** 通过资源文件初始化数据源
     * 
     * @param bundleName
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static DataSource initBy(String bundleName)
    {
        DataSource dataSource = new DataSource();
        ResourceBundle config = ResourceBundle.getBundle(bundleName);
        PoolProperties p = new PoolProperties();
        p.setUrl(config.getString("jdbc.url"));
        p.setDriverClassName(config.getString("jdbc.driver"));
        p.setUsername(config.getString("jdbc.username"));
        p.setPassword(config.getString("jdbc.password"));
        p.setFairQueue(true);// 异步获取连接,必须将fairQueue设置为true
        p.setJmxEnabled(true);
        p.setTestWhileIdle(false);
        p.setTestOnBorrow(true);
        p.setValidationQuery("SELECT 1");
        p.setTestOnReturn(false);
        p.setValidationInterval(30000);
        p.setTimeBetweenEvictionRunsMillis(30000);
        p.setMaxActive(100);
        p.setInitialSize(10);
        p.setMaxWait(10000);
        p.setRemoveAbandonedTimeout(60);
        p.setMinEvictableIdleTimeMillis(30000);
        p.setMinIdle(10);
        p.setLogAbandoned(true);
        p.setRemoveAbandoned(true);
        p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
        dataSource.setPoolProperties(p);
        return dataSource;
    }
}
